package com.chuangke.elasticsearch.controller;

import cn.hutool.core.util.StrUtil;
import com.chuangke.common.http.HttpResult;
import com.chuangke.elasticsearch.model.ElasticData;
import com.chuangke.elasticsearch.model.ElasticEntity;
import com.chuangke.elasticsearch.service.ElasticsearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.*;

/**
 * 数据管理
 * 
 * @Description
 */
@RequestMapping("/elasticMgr")
@RestController
public class ElasticMgrController {

//	@Autowired
//	private ElasticsearchService baseElasticService;

	/**
	 * @Description 新增数据
	 * @param elasticDataVo
	 */
	@PostMapping(value = "/add")
	public HttpResult add(@RequestBody ElasticData elasticDataVo) {
		if (!StrUtil.isNotBlank(elasticDataVo.getIdxName())) {
			return HttpResult.error("索引为空，不允许提交");
		}
		ElasticEntity elasticEntity = new ElasticEntity<>();
		elasticEntity.setId(elasticDataVo.getElasticEntity().getId());
		elasticEntity.setData(elasticDataVo.getElasticEntity().getData());

//		baseElasticService.insertDocument(elasticDataVo.getIdxName(), elasticEntity);

		return HttpResult.ok();
	}

	/**
	 * @Description 删除
	 * @param elasticDataVo
	 */
	@PostMapping(value = "/delete")
	public HttpResult delete(@RequestBody ElasticData elasticDataVo) {
		if (!StrUtil.isNotBlank(elasticDataVo.getIdxName())) {
			return HttpResult.error("索引为空，不允许删除");
		}
//		baseElasticService.deleteDocument(elasticDataVo.getIdxName(), elasticDataVo.getElasticEntity().getId());
		return HttpResult.ok();
	}

//	/**
//	 * @Description
//	 * @param index 初始化Location区域，写入数据。
//	 */
//	@GetMapping(value = "/init/{index}")
//	public HttpResult addLocation(@PathVariable(value = "index") String index) {
//		List<Map<String, Object>> locations = folderMethod();
//		List<ElasticEntity> elasticEntitys = new ArrayList<>(locations.size());
//		for (Map<String, Object> data : locations) {
//			ElasticEntity elasticEntity = new ElasticEntity();
//			elasticEntity.setId(data.get("id").toString());
//			elasticEntity.setData(data);
//			elasticEntitys.add(elasticEntity);
//		}
//		baseElasticService.insertBatch(index, elasticEntitys);
//		return HttpResult.ok();
//	}

	public static List<Map<String, Object>> folderMethod() {
		List<Map<String, Object>> fileList = new ArrayList<>();

		File file = new File("E:\\Java面试突击-V1.0.pdf");
		Map fileMap = new HashMap<>();
		fileMap.put("id", UUID.randomUUID());
//		fileMap.put("content", TikaUtil.getContent(file));
		fileList.add(fileMap);
		return fileList;
	}

//	/**
//	 * @Description
//	 * @param queryVo 查询实体对象
//	 */
//	@GetMapping(value = "/get")
//	public HttpResult get(@RequestBody QueryVo queryVo) {
//		if (!StrUtil.isNotBlank(queryVo.getIdxName())) {
//			return HttpResult.error("索引为空，不允许提交");
//		}
//
//		Class<?> clazz = ElasticUtil.getClazz(queryVo.getClassName());
//		Map<String, Object> params = queryVo.getQuery().get("match");
//		Set<String> keys = params.keySet();
//		MatchQueryBuilder queryBuilders = null;
//		for (String ke : keys) {
//			queryBuilders = QueryBuilders.matchQuery(ke, params.get(ke));
//		}
//		if (null != queryBuilders) {
//			SearchSourceBuilder searchSourceBuilder = ElasticUtil.initSearchSourceBuilder(queryBuilders);
//			List<?> data = baseElasticService.search(queryVo.getIdxName(), searchSourceBuilder, clazz);
//			return HttpResult.ok(data);
//		}
//		return HttpResult.ok();
//	}

}
